package com.nineclock.auth.processor;

import com.nineclock.auth.entity.IntegrationAuthenticationEntity;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.system.dto.SysUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 账号密码模式认证处理器
 */
@Component
public class UsernameAndPasswordAuthenticationProcessor implements AuthenticationProcessor {
    @Autowired
    SysUserFeign userFeign;
    @Override
    public SysUserDTO authenticate(IntegrationAuthenticationEntity entity) {
        String username = entity.getAuthParameter("username");
        String password = entity.getAuthParameter("password");
        if (username == null || password == null){
            throw new NcException(ResponseEnum.INVALID_USERNAME_PASSWORD);
        }
        SysUserDTO userDTO = userFeign.queryUser(username).getData();
        if (userDTO == null){
            throw new NcException(ResponseEnum.USER_NOT_FOUND);

        }
        return userDTO;
    }

    @Override
    public boolean support(IntegrationAuthenticationEntity entity) {
        if (entity != null && entity.getAuthType() == null){
            return true;
        }
        return false;
    }
}
